package top.suven.base.util.utilize;

import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadLocalRandom;


@Component
public class GUID {
	private long maxSeq = 16382L;
	private long rand = random();
	private long seq = 0L;
	private long time = System.nanoTime();

	public long next() {
		long value = (random() << 64L);
		synchronized (this) {
			if (seq < maxSeq) {
				seq++;
			} else {
				seq = 0L;
				rand = random();
				time = System.nanoTime();
			}
			value += (seq << 32L);
			value += rand << 16L;
			value += time;
		}
		return value;
	}

	private long random() {
		return ThreadLocalRandom.current().nextLong(128, 2048);
	}
	
	
}
